package com.sufen.seckill.validator;

import com.sufen.seckill.utils.ValidatorUtil;
import org.thymeleaf.util.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author sufen
 * @Date 2021/10/19 11:09
 * @Version 1.0
 * 功能描述：手机号码校验的类
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    public boolean required=false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required=constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isMobile(s);
        }else{
            if(StringUtils.isEmpty(s)){
                return true;
            }else{
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
